package cn.czg.cms.controller;

import cn.czg.cms.common.ResultT;
import cn.czg.cms.entity.dto.CommentDTO;
import cn.czg.cms.entity.dto.SimpleCom;
import cn.czg.cms.entity.req.CommentRequest;
import cn.czg.cms.service.CommentService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/comments")
public class CommentController {

    @Autowired
    private CommentService commentService;

    // 添加评论
    @PostMapping("/add")
    public ResultT<?> addComment(@RequestBody CommentRequest req) {
        commentService.addComment(req.getPostId(), req.getParentId(), req.getUserId(), req.getContent());
        return ResultT.success();
    }

    // 获取评论和回复
    @GetMapping("/list")
    public ResultT<List<CommentDTO>> listComments(@RequestParam Long postId,
                                                  @RequestParam(defaultValue = "1") int page,
                                                  @RequestParam(defaultValue = "10") int size) {
        return ResultT.success(commentService.getCommentsWithReplies(postId, page, size));
    }


    @GetMapping("/simpleCom")
    public ResultT<IPage<SimpleCom>> simpleCom(@RequestParam Long postId,
                                               @RequestParam(defaultValue = "1") int page,
                                               @RequestParam(defaultValue = "10") int size) {

        return ResultT.success(commentService.simpleCom(postId, page, size));
    }
}
